package com.madev.isellcrazyitems;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.madev.isellcrazyitems.entity.Product;
import com.madev.isellcrazyitems.entity.Statement;
import com.madev.isellcrazyitems.handler.DatabaseHandler;
import com.madev.sales.R;


public class AddRecord extends Activity implements OnClickListener {

	private Button btn_addrecord;
	private EditText txtpname, txtpprice, txtpid, txtstock, txturl;
	DatabaseHandler db;
	DatabaseHandler sc;
	Product pm;
	Statement st;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_addrecord:

			if (txtpname.getText().toString().equals("")
					|| txtpprice.getText().toString().equals("")) {
				Toast.makeText(AddRecord.this, "Please add values..",
						Toast.LENGTH_LONG).show();
			} else {

				db = new DatabaseHandler(getApplicationContext());
				db.getWritableDatabase();
				pm = new Product();
				pm.productID = txtpid.getText().toString();
				pm.productName = txtpname.getText().toString();
				pm.productPrice = txtpprice.getText().toString();
				pm.productStock = txtstock.getText().toString();
				pm.productImagePath = txturl.getText().toString();
				
				Log.i("idno,productname,productprice,productstock", "" + pm.productID + ""
						+ pm.productName + "" + pm.productPrice + "" + pm.productStock + "" + pm.productImagePath);
				db.addProduct(pm);
				
				sc = new DatabaseHandler(getApplicationContext());
				sc.getWritableDatabase();
				st = new Statement();
				st.statement = ("INSERT INTO Products (productIdNo, productName, productPrice, productStock, productImagePath) VALUES " +
						"('" + pm.getProductID() + "','" + pm.getProductName() + "','" + pm.getProductPrice() + "','" + pm.getProductStock() + "','" + pm.getProductImagePath() + "');");
				sc.addStatement(st);
				
				Toast.makeText(AddRecord.this, "Record Added successfully.",
						Toast.LENGTH_LONG).show();
				finish();
			}
			break;

		default:
			break;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addrecord);

		txtpname = (EditText) findViewById(R.id.txtpname);
		txtpprice = (EditText) findViewById(R.id.txtpprice);
		btn_addrecord = (Button) findViewById(R.id.btn_addrecord);
		txtstock = (EditText) findViewById(R.id.txtstock);
		txtpid = (EditText) findViewById(R.id.txtpid);
		txturl = (EditText) findViewById(R.id.txturl);
		btn_addrecord.setOnClickListener(this);

	}
}
